package servlet;

import bean.UserBean;
import dao.UserDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    /**
     * 实现注册账号的功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");//防止中文乱码


        //获取账号和密码
        String name =request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("账号："+name);

        UserBean userBean=new UserBean(name,password);

        UserDao userDao=new UserDao();
        int sta=userDao.addUser(userBean);
        request.getSession().setAttribute("staId", "0");
        if(sta==0){
            request.getSession().setAttribute("sta", "注册失败，改账号已经存在");
            response.sendRedirect("zhuce.jsp");
        }else{
            request.getSession().setAttribute("staId", "1");
            request.getSession().setAttribute("sta", "注册成功，请登录...");
            response.sendRedirect("zhuce.jsp");

        }


    }
}
